package com.smile.frame.common.util;

import org.springframework.beans.BeanUtils;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author LGC
 * @date 2021/7/28 20:02

 */
public class CopyUtil {

    public static <S, D> D copy(S srcObject, Class<D> destClassType) {
        D dest = null;
        if (srcObject == null) {
            return null;
        }
        try {
            dest = destClassType.newInstance();
            BeanUtils.copyProperties(srcObject, dest);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return dest;
    }

    public static <S, D> List<D> copyList(List<S> srcObjectList, Class<D> destClassType) {
        return srcObjectList.stream().map(srcObject -> copy(srcObject, destClassType)).collect(Collectors.toList());
    }

}
